/**
 * @description Model Manifest
 *
 * @author Xiongmao
 */

var db = require('../db');
const parseJSON = require('../utils/parse-json-safe');

module.exports = db.model('Role', {
    tableName: 'roles',
    hasTimestamps: true,
    users() {
        return this.hasMany('User', 'role_id');
    },
    parse(attrs) {
        if('menus' in attrs) {
            attrs.menus = parseJSON(attrs.menus);
        }
        return attrs;
    },

    format(attrs) {
        if('menus' in attrs) {
            const menus = attrs.menus || null;

            attrs.menus = JSON.stringify(menus);
        }
        return attrs;
    },
});
